package ejb.Servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Wei, Yu, Igor Utility class for processing geocoordinate datafile in
 *         CSV format. The class splits a reversely geocoded street address into
 *         fields of State, City and Street.
 */
public final class GeoCSVProcesser {

	/**
	 * Private constructor of the utility class.
	 */
	private GeoCSVProcesser() {
	}
	/**
	 * The main of the utility class.
	 * @param args the system arguments.
	 * @throws IOException.
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader inStream = null;
		PrintWriter outStream = null;
		try {
			inStream = new BufferedReader(new FileReader("geo.txt"));
			outStream = new PrintWriter(new BufferedWriter(new FileWriter(
					"geo_output.txt")));
			String line;
			inStream.readLine(); // skip the header line
			outStream
					.println("GEO_ID\tREGION\tDIVISION\tSTATEFP\tSTATENS\tSTUSPS"
							+ "\tNAME\tLAT\tLON\tLAT," +
							"LON\tID\tADDRESS\tSTREET\tCITY\tCOUNTRY");
			// print the header line in the output file
			String country;
			String city;
			String street;
			while ((line = inStream.readLine()) != null) {
				// GEO_ID REGION DIVISION STATEFP STATENS STUSPS NAME LAT LON
				// LAT,LON ID ADDRESS
				line = line.replaceAll("\"", "");
				line = line.replaceAll(", ", ",");
				final String[] values = line.split("\t");
				final String[] addresses = values[11].split(",");
				country = "";
				city = "";
				street = "";
				country = addresses[addresses.length - 1];
				if (addresses.length >= 4) {
					city = addresses[addresses.length - 3];
					for (int i = 0; i < addresses.length - 3; i++) {
						street += addresses[i];
					}
					// System.out.println(country+"\t"+zip+"\t"+city+"\t"+street);
				} else if (addresses.length == 3) {
					city = addresses[0];
				} else {
					city = addresses[0];
				}
				String outLine = line;
				outLine += "\t" + street + "\t" + city + "\t" + country;
				System.out.println(outLine);
				outStream.println(outLine);
			}
			System.out.print("Done!");
		} finally {
			if (inStream != null) {
				inStream.close();
			}
			if (outStream != null) {
				outStream.close();
			}
		}
	}
}
